package Filter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class Time implements HttpSessionListener, HttpSessionAttributeListener {

	// 参数
	ServletContext sc;
	long time;
	long time1;

	Map<String, Long> map = new HashMap<String, Long>();

	// 新建一个session时触发此操作
	public void sessionCreated(HttpSessionEvent se) {
		sc = se.getSession().getServletContext();
		System.out.println("新建一个session");
	}

	// 销毁一个session时触发此操作
	public void sessionDestroyed(HttpSessionEvent se) {

		System.out.println("销毁一个session");

	}

	// 在session中添加对象时触发此操作，在list中添加一个对象
	public void attributeAdded(HttpSessionBindingEvent sbe) {
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd ss:mm");
		String root = (String) sbe.getSession().getAttribute("user_name");
		time = System.currentTimeMillis();
		System.out.println("登陆时间" + format.format(date));
		System.out.println("用户" + root);

	}

	// 修改、删除session中添加对象时触发此操作
	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		String root = (String) arg0.getSession().getAttribute("user_name");
		Date date = new Date();
		time1 = System.currentTimeMillis();
		long tiem = time1 - time;
		map.put(root, tiem);
		System.out.println(tiem);
		System.out.println("5555555");
	}

	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		System.out.println("77777777");
	}

}
